<template>
  <a-layout class="index">
    <a-layout-sider collapsible v-model="collapsed" >
       <div class="logo" >
        <img src="@/assets/logo.png" />
        <h1>DMD-1</h1>
      </div> 
      <default-menu @handle-click="handleClick"></default-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #eee; padding: 0 15px">
        <!-- <img src="@/assets/logo.png" class="header-logo"/> -->

        <Logout class="header-item"/>
        <PasswordChange class="header-item"/>
        <!-- <LanguageSwitch class="header-item"/> -->
        <Greeting class="header-item"/>
      </a-layout-header>
      <a-layout-content :style="{background: '#fff', minHeight: '280px'}">
        <a-tabs
          hideAdd
          v-model="activeKey"
          type="editable-card"
          @edit="onEdit"
        >
          <a-tab-pane v-for="pane in panes" :tab="$t(pane.title)" :key="pane.key" :closable="pane.closable" style="padding: 0 10px">
            <component :is="pane.key" :activeKey="activeKey"></component>
          </a-tab-pane>
        </a-tabs>
      </a-layout-content>
      <a-layout-footer :style="{ textAlign: 'center', padding: '5px 20px' }">
        <span class="copy-right">
          DMD-1 © 2015-{{ yearEnd }} 赛福基因
        </span>
        <span>
          京ICP备 <a href="http://beian.miit.gov.cn/" target="_blank">16032624号-3 </a>
        </span>
        <span class="version-right">
          软件发布版本号：V1&nbsp;&nbsp;&nbsp;&nbsp;软件完整版本号：V1.0.0.0
        </span>
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script>

export default {
  mounted() {
    this.$store.dispatch('loadUserInfo')
    this.$store.dispatch('loadRsaPublicKey')
    this.$store.dispatch('loadDict')
    // clientHeight
    this.$store.commit('setClientHeight', `${document.documentElement.clientHeight}`)
    window.onresize = ()=>{
      this.$store.commit('setClientHeight', `${document.documentElement.clientHeight}`)
    }
  },
  data(){
    const panes = [
      {title: 'dashboard', key: 'Dashboard', closable: false}
    ]
    return {
      activeKey: panes[0].key,
      panes,
      newTabIndex: 0,
      collapsed: false,
      yearEnd: new Date().getFullYear()
    }
  },
  methods: {
    handleVisibilitychange() {
      let visibilityState = document.visibilityState
      this.$store.commit('setDocumentVisibility', visibilityState == 'visible')
    },
    // 点击菜单
    handleClick (item) {
      if (this.tabExists(item.key)) {
        this.activeKey = item.key
        return
      } 

      // 增加tab
      this.addTab(item)
    },
    addTab (item) {
      this.panes.push({ title: item.title,  key: item.key })
      this.activeKey = item.key
    },
    tabExists (key) {
      let result = false
      for(let i=0;i<this.panes.length;i++){
        let item = this.panes[i]
        if(item.key == key) {
          result = true
          break
        }
      }
      return result
    },
    onEdit (targetKey, action) {
      this[action](targetKey)
    },
    remove (targetKey) {
      let activeKey = this.activeKey
      let lastIndex
      this.panes.forEach((pane, i) => {
        if (pane.key === targetKey) {
          lastIndex = i - 1
        }
      })
      const panes = this.panes.filter(pane => pane.key !== targetKey)
      if (lastIndex >= 0 && activeKey === targetKey) {
        activeKey = panes[lastIndex].key
      }
      this.panes = panes
      this.activeKey = activeKey
    }
  }
}
</script>
<style>
.index {
  min-height: 100vh;
}
.index .logo {
  height: 64px;
  background: rgba(255,255,255,.2);
  overflow: hidden;
  padding-left: 12px;
  line-height: 64px;

}
.index .logo img {
  height: 48px;
  display:inline-block;
  position: relative;
  top: -3px;
}
.index .logo h1 {
  color: #fff;
  margin: 0 0 0 8px;
  font-size: 16px;
  display:inline-block;
}
.index .header-item {
  float: right;
  margin-left: 8px;
  color: rgba(0, 0, 0, 0.65)
}
.index .copy-right {
  float: left;
}
.index .version-right {
  float: right;
}
</style>
